package com.rqf.ls.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.ls.service.ICommonDataService;
import com.rqf.ls.service.ISignUpService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("SignUpService")
public class SignUpService implements ISignUpService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService; 
	
	@Override
	public Map<String, Object> add(String phoneNo, Map<String, Object> input){
		Map<String, Object> inputParamMap = new HashMap<String, Object>();
		inputParamMap.put("activityName", input.get("activityName"));//VARCHAR
		inputParamMap.put("locationAddr", input.get("locationAddr"));//VARCHAR
		inputParamMap.put("locationDetail", input.get("locationDetail"));//VARCHAR
		inputParamMap.put("startTime", input.get("startTime"));//DATE
		inputParamMap.put("endTime", input.get("endTime"));//DATE
		inputParamMap.put("filePath", input.get("filePath"));//VARCHAR
		inputParamMap.put("phoneNo", phoneNo);//VARCHAR
		inputParamMap.put("note", input.get("note"));//VARCHAR
		inputParamMap.put("userType", input.get("userType"));//VARCHAR
		inputParamMap.put("players", input.get("players"));//INT
		inputParamMap.put("longitude", input.get("longitude"));//FLOAT
		inputParamMap.put("latitude", input.get("latitude"));//FLOAT
		
	Map<String, Object> execute = new SimpleJdbcCall(jdbcTemplate)
		.withProcedureName("SignUp_Add")
		.declareParameters(
					new SqlParameter("activityName",java.sql.Types.VARCHAR),
					new SqlParameter("locationAddr",java.sql.Types.VARCHAR),
					new SqlParameter("locationDetail",java.sql.Types.VARCHAR),
					new SqlParameter("startTime",java.sql.Types.DATE),
					new SqlParameter("endTime",java.sql.Types.DATE),
					new SqlParameter("filePath",java.sql.Types.VARCHAR),
					new SqlParameter("phoneNo",java.sql.Types.VARCHAR),
					new SqlParameter("note",java.sql.Types.VARCHAR),
					new SqlParameter("userType",java.sql.Types.VARCHAR),
					new SqlParameter("players",java.sql.Types.INTEGER),
					new SqlParameter("longitude",java.sql.Types.FLOAT),
					new SqlParameter("latitude",java.sql.Types.FLOAT),
					new SqlOutParameter("id",java.sql.Types.INTEGER),
					new SqlOutParameter("isSuccess",java.sql.Types.INTEGER)
		).execute(inputParamMap);
	return execute;	
	}

	@Override
	public Boolean update(Integer id, Map<String, Object> input) {
		// TODO
		return null;
	}

	@Override
	public Boolean delete(Integer id) {
		// TODO
		return null;
	}
	
	@Override
	public List<Map<String, Object>> getList() {
		String sql = "Select * from V_SIGNUP_SELECT order by createTime desc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	@Override
	public List<Map<String, Object>> getList(String phoneNo,String userType) {
		String sql = "Select * from V_SIGNUP_SELECT where USERTYPE='%s' %s order by createTime desc";
		if (!StringUtils.isEmpty(phoneNo)) {
			sql = String.format(sql, userType, "and phoneNo='"+phoneNo+"'");
		}
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	@Override
	public List<Map<String, Object>> getList(String phoneNo, 
			String whereStr, String sortStr) {
		String sql = "Select * from V_SIGNUP_SELECT %s order by createTime desc";
		if (StringUtils.isEmpty(phoneNo)) {
			sql = String.format(sql, "where phoneNo='"+phoneNo+"'");
		}
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	@Override
	public DataVector<Map<String, Object>> getList(String phoneNo,String userType,
			RequestInput requestInput) {
		String table = "V_SIGNUP_SELECT";
		String _whereStr = "phoneNo='"+phoneNo+"' and USERTYPE='"+userType+"'";
		String sortStr = "createTime desc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(table, _whereStr, sortStr, requestInput);
		return listData;
	}
	@Override
	public Map<String, Object> getForm(Integer id) {
		String sql = "Select * from V_SIGNUP_SELECT %s";
		String realSql = String.format(sql, "where id="+id);
		List<Map<String, Object>> list = jdbcTemplate.queryForList(realSql);
		/*String sql = "Select y.*,l.username from yuyue y left outer join lsuser l on l.phone=y.whoyuyueid where y.id=%s";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, orderId));
		if (list.size() > 0) {
			return list.get(0);
		}else {
			return new HashMap<String, Object>();
		}*/
		if (list.size() > 0) {
			return list.get(0);
		}else {
			return new HashMap<String, Object>();
		}
	}

	/**
	 * 获取公益活动照片路径
	 * 
	 * @param id
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月9日
	 */
	@Override
	public String getImgPath(Integer id) {
		String path = "";
		String sql = "Select * from ATTACHMENT where %s";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String
				.format(sql, "SIGNUPID=" + id));
		if (list.size() > 0) {
			Map<String, Object> map = list.get(0);
			Object pathObj = map.get("FILEPATH");
			Object uuidObj = map.get("UUID");
			Object typeObj = map.get("TYPE");
			if (pathObj != null && uuidObj != null && typeObj != null) {
				path += pathObj.toString() +"\\" + uuidObj.toString()
						+"."+ typeObj.toString();
			}
		}
		return path;
	}
}
